# Set working directory to JOP Replication files folder 
#setwd("/Users/rithika/Dropbox/Dissertation/JOP Replication files/")

library(haven)
library(ggpubr)
library(ggplot2)
margins_did <- read_dta("DATA FILES TO SHARE/TEMP_FILES/always_mig.dta")
margins_did$group <- as.factor(margins_did$group)
levels(margins_did$group) <- c("Wave 2 Migrants","Always Migrants")
margins_did$Variable <- as.factor(margins_did$Variable)
levels(margins_did$Variable)<- c("Mobility", "Cash", "Decision-making", "Political Index")

margins_did$year <- as.factor(margins_did$year)
levels(margins_did$year) <- c("Wave 1","Wave 2")

p1<- 
  ggplot(margins_did[c(1,2,9,10),],aes(x=year, y=mean, group=group)) +
  geom_point(aes(color=group))+geom_line(aes(linetype=group,color=group))+
  geom_pointrange(aes(ymin= cl, ymax= cu,color=group))+
  labs(
    x="Time period", 
    title="Autonomous Mobility")+theme_minimal(base_size = 15)+
  theme(legend.text = element_text(size=12))+
  scale_linetype_manual(values=c("twodash", "dashed"))+
  scale_color_manual(values=c("blue", "red"))+
  
  annotate('text',x= 0.9, y = c(0.35), label='atop(bold("***"))', parse=TRUE, color = c("black")) 
p1

p2<- 
  ggplot(margins_did[c(3,4,11,12),],aes(x=year, y=mean, group=group)) +
  geom_point(aes(color=group))+geom_line(aes(linetype=group,color=group))+
  geom_pointrange(aes(ymin=cl, ymax=cu,color=group))+
  labs(
    x="Time period", 
    title="Access to Cash")+theme_minimal(base_size = 15)+
  theme(legend.text = element_text(size=12))+
  scale_linetype_manual(values=c("twodash", "dashed"))+
  scale_color_manual(values=c("blue", "red"))+
  
  annotate('text',x= 0.9, y = c(0.85), label='atop(bold("***"))', parse=TRUE, color = c("black")) 
p2

p3<- 
  ggplot(margins_did[c(5,6,13,14),],aes(x=year, y=mean, group=group)) +
  geom_point(aes(color=group))+geom_line(aes(linetype=group,color=group))+
  geom_pointrange(aes(ymin=cl, ymax=cu,color=group))+
  labs(
    x="Time period", 
    title="Decision-Making")+theme_minimal(base_size = 15)+
  theme(legend.text = element_text(size=12))+
  scale_linetype_manual(values=c("twodash", "dashed"))+
  scale_color_manual(values=c("blue", "red"))+
  annotate('text',x= 0.9, y = c(0.80), label='atop(bold("*"))', parse=TRUE, color = c("black")) 
p3

p4<- 
  ggplot(margins_did[c(7,8,15,16),],aes(x=year, y=mean, group=group)) +
  geom_point(aes(color=group))+geom_line(aes(linetype=group,color=group))+
  #geom_pointrange(aes(ymin=Mean-se, ymax=Mean+se,color=Group))+
  labs(
    x="Time period", 
    title="Political Engagement")+theme_minimal(base_size = 15)+
  theme(legend.text = element_text(size=12))+
  scale_linetype_manual(values=c("twodash", "dashed"))+
  scale_color_manual(values=c("blue", "red"))
p4



p5<- ggarrange(p1,p2,p3,p4, ncol=2, nrow=2,common.legend = TRUE, legend="bottom")
p5

ggsave("OUTPUT/GRAPHS/Figure_A3.png", plot = p5, width = 8, height = 6, dpi = 150, units = "in")